<template>
	<el-container v-loading="loading">
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" v-auth="'service_provider:index:edit'" icon="el-icon-plus" @click="handleData('add')">新建服务商</el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.provider_code" placeholder="服务商编码" clearable></el-input>
						<el-input v-model="search.provider_name" placeholder="服务商名称" clearable></el-input>
						<el-select v-model="search.provider_type" placeholder="服务商类型" style="min-width:25%;" clearable>
							<el-option v-for="item in providerTypeList" :key="item.provider_type" :label="item.provider_type" :value="item.provider_type">
							</el-option>
						</el-select>
						<el-select v-model="search.status" placeholder="状态" style="min-width:19%;" clearable>
							<el-option v-for="item in statusList" :key="item.status" :label="item.status_name" :value="item.status">
							</el-option>
						</el-select>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						<el-button type="info" @click="resetsearch">重置</el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort
						 remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="ID" prop="provider_id" width="100"></el-table-column>
					<el-table-column label="服务商编码" prop="provider_code" width="150"></el-table-column>
					<el-table-column label="服务商名称" prop="provider_name" width="150"></el-table-column>
					<el-table-column label="服务商类型" prop="provider_type" width="150"></el-table-column>
					<el-table-column label="联系人" prop="link_user" width="150"></el-table-column>
					<el-table-column label="联系电话" prop="link_mobile" width="150"></el-table-column>
					<el-table-column label="联系邮箱" prop="link_email" width="150"></el-table-column>
					<el-table-column label="备注" prop="remark" width="200"></el-table-column>
					<el-table-column label="状态" prop="status" width="80">
						<template #default="scope">
							<el-tag v-if="scope.row.status==1" type="success">启用</el-tag>
							<el-tag v-if="scope.row.status==0" type="danger">停用</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="创建时间" prop="created_at" width="170"></el-table-column>
					<el-table-column label="创建人" prop="created_by" width="170"></el-table-column>
<!--					<el-table-column label="启用停用" prop="status" width="80">-->
<!--						<template #default="scope">-->
<!--							<el-switch v-model="scope.row.status" @change="changeSwitch($event, scope.row)"-->
<!--									   :loading="scope.row.$switch_status" active-value="1" inactive-value="0"></el-switch>-->
<!--						</template>-->
<!--					</el-table-column>-->
					<el-table-column label="操作" fixed="right" align="right" width="160">
						<template #default="scope">
							<el-button-group>
								<!--								<el-button text type="primary" size="small"-->
								<!--										   @click="handleData('show',scope.row, scope.$index)">查看-->
								<!--								</el-button>-->
								<el-button text type="primary" v-auth="'service_provider:index:edit'" size="small"
										   @click="handleData('edit',scope.row, scope.$index)">编辑
								</el-button>
								<el-button v-if="scope.row.status==0" text type="primary" v-auth="'service_provider:index:edit'" size="small"
										   @click="changeProviderStatus('change_status',scope.row, scope.$index)">启用
								</el-button>
								<el-button v-if="scope.row.status==1" text type="primary" v-auth="'service_provider:index:edit'" size="small"
										   @click="changeProviderStatus('change_status',scope.row, scope.$index)">停用
								</el-button>
								<!--								<el-button v-if="scope.row.status==0" text type="primary" v-auth="'service_provider:index:delete'" size="small"-->
								<!--										   @click="deleteProvider('delete',scope.row, scope.$index)">删除-->
								<!--								</el-button>-->
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
	import saveDialog from './save.vue'

	export default {
		name: 'service_provider_index',
		components: {
			saveDialog
		},
		data() {
			return {
				dialog: {
					save: false
				},
				showGroupLoading: false,
				loading: false,
				apiObj: this.$API['service_provider.service_provider'].index,
				selection: [],
				statusList: [
					{status: 0, status_name: "停用"},
					{status: 1, status_name: "启用"},
				],
				providerTypeList: [],
				search: {
					provider_code: null,
					provider_name: null,
					status: null,
					provider_type: null,
				}
			}
		},
		mounted() {
			//获取单位列表
			this.getProviderTypeList()
		},
		methods: {
			getProviderTypeList() {
				this.$API['service_provider.service_provider'].getProviderTypeList.get().then((res) => {
					if (res.code == 0) {
						this.providerTypeList = res.data;
					}
				}).catch((msg) => {
					this.$alert(msg, "提示", {type: 'error'})
				})
			},
			//添加
			add() {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			handleData(mode, row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open(mode).setData(row)
				})
			},
			//启用停用
			changeProviderStatus(mode, row) {
				console.log(mode, row);
				var tip = "确认启用当前服务商吗？确认后变更为启用";
				var val = 1;
				if (row['status']==1) {
					tip = '确认停用当前服务商吗？确认后变更为停用';
					val = 0;
				}
				this.$confirm(tip, '提示', {
					type: 'warning',
					confirmButtonText: '确定',
					confirmButtonClass: 'el-button--danger'
				}).then(() => {
					this.$API['service_provider.service_provider'].changeStatus.post({provider_id: row.provider_id, status: val})
						.then((res) => {
							if (res.code == 0) {
								this.$refs.table.refresh()
								this.$message.success("操作成功")
							} else {
								this.$alert(res.message, "提示", {type: 'error'})
							}
						})
						.catch((msg) => {
							this.$alert(msg, "提示", {type: 'error'})
						})
						.finally(() => {

						});
				}).catch(() => {
				})

			},
			//删除
			deleteProvider(mode, row) {
				console.log(mode, row);
				this.$confirm(`注：确认删除此用户吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '确定',
					confirmButtonClass: 'el-button--danger'
				}).then(() => {
					this.$API['user.user'].deleteUser.post({provider_id: row.provider_id})
						.then((res) => {
							if (res.code == 0) {
								this.$refs.table.refresh()
								this.$message.success("操作成功")
							} else {
								this.$alert(res.message, "提示", {type: 'error'})
							}
						})
						.catch((msg) => {
							this.$alert(msg, "提示", {type: 'error'})
						})
						.finally(() => {

						});
				}).catch(() => {
				})

			},

			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			//表格内开关
			changeSwitch(val, row) {
				row.$switch_status = true;
				this.$API['user.user'].changeStatus.post({status: val, provider_id: row.provider_id})
					.then((res) => {
						if (res.code == 0) {
							this.$emit('success', this.form, this.mode)
							this.$message.success("操作成功")
							row.status = val;
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					})
					.catch((msg) => {
						this.$alert(msg, "提示", {type: 'error'})
					})
					.finally(() => {
						row.$switch_status = false;
					});
			},
			//搜索
			upsearch() {
				this.$refs.table.upData(this.search)
			},
			//重置
			resetsearch() {
				this.search = {
					provider_code: null,
					provider_name: null,
					status: null,
					provider_type: null,
				}
			},
			//本地更新数据
			handleSuccess() {
				this.dialog.save = false
				this.$refs.table.reload()
			},
		}
	}
</script>

<style>
</style>
